import Taro, {Component} from '@tarojs/taro'
import {View, Image, Text} from '@tarojs/components'
import Navbar from "../../components/Navbar";
import login from '../../utils/login'
import {connect} from '@tarojs/redux'
import * as actions from '../../actions/wx'
import './index.scss'

@connect(state => state.wx, {...actions})
class Auth extends Component {
  static options = {
    addGlobalClass: true
  }

  config = {
    navigationBarTitleText: '登录',
    enablePullDownRefresh: false,
    navigationStyle: "custom"
  }

  componentWillMount() {

  }

  componentWillPreload() {

  }

  componentDidMount() {

  }

  componentDidHide() {
  }

  componentCatchError() {
  }

  componentDidShow() {
    // const token = Taro.getStorageSync('token')
    // if(token){
    //   Taro.navigateTo({
    //     url: `/pages/tabs/index`
    //   })
    // }
  }

  render() {
    return (
      <View>
        <Navbar type="icon" fixed={true} title={'登录'}></Navbar>
        <View className="auth_page">

          <View className="auth_box">
            <Image src="https://qiniuqianshop.fortunemy.top/qian.jpg" className="auth_box_logo" lazyLoad>

            </Image>
            <Text className="auth_box_title">
              倩~全球购
            </Text>
            <Text className="auth_box_desc">
              检测到您未登录,本软件部分功能需要登录授权登录，
            </Text>
            <Text className="auth_box_desc auth_box_desc2">
              如需继续使用此功能，请点击'微信快捷登录'按钮，一键登录小程序
            </Text>
            <Button className='auth_box_btn' openType='getUserInfo'
                    onGetUserInfo={this.login.bind(this)}>微信快捷登录</Button>
          </View>
        </View>
      </View>
    )
  }

  async login() {
    const result = await login()
    const {userInfo, code} = result
    if (userInfo) {
      this.props.dispatchWxLogin(code, userInfo).then(token => {
        this.props.dispatchWxUser()
        const {redirect} = this.$router.params
        if (redirect) {
          Taro.navigateTo({
            url: redirect
          })
        } else {
          Taro.navigateBack()
        }
      }, err => {
        Taro.navigateBack()
      })
    }
  }
}

export default Auth
